$(function(){
	//全选
	$(".js-check-all").click(function(){
		$(".js-select-tr").attr("checked",true);
	})
	// 反选
	$(".js-versa-check").click(function(){
         for(i=0;i<$(".js-select-tr").length;i++){
             if($(".js-select-tr")[i].checked==true){
                $(".js-select-tr")[i].checked=false;
             } else{
             	$(".js-select-tr")[i].checked=true
             }
             
         };
     });
	// 重置
	$(".js-reset").click(function(){
		$(".tc-spread-l").find(".group_list").find("input").attr("checked",false)
	})
	$(".fied").show();
	var industryId = GetQueryString('industryId');
	var keywordstr = GetQueryString('keywordstr');
	var inversestr = GetQueryString('inversestr');
	
	$.get('/material/getMaterial', function(result) {
		//填充表格,materialList
		material = result.materialList
		if(material==null){
			$('#materialTable').append("<tr class=\"group_list\"><td>暂无匹配信息</td></tr>");
			$(".fied").hide();
			return;
		}
		var star = "";
		for(i=0;i<material.length;i++){
			if(material[i].keyword!=""&&material[i].keyword!=null)
			{
				
				if(material[i].clickRate==null||material[i].clickRate==0){
					star = "<td><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"></td>";
				}else if(0<material[i].clickRate<0.3){
					star = "<td><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"></td>";
				}else{
					star = "<td><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"><img src=\"../img/star.png\" width=\"30px\" height=\"30px\"></td>";
				}
				
				$('#materialTable').append("<tr class=\"group_list\" id=\""+i+"\">"	
				+"<td><input type=\"checkbox\" name=\"check\" value=\""+i+"\" class=\"js-select-tr\"><label>"+material[i].promotionPlanName+"</label></td>"
				+"<td>"+material[i].promotionUnitName+"</td>"
				+"<td> "+material[i].keyword+"</td>"
				+"<td> "+material[i].totalnumberConsult+"</td>"
				+star+"</tr>");										
			}
		}
		$(".fied").hide();
		var table =document.getElementById("materialTable");
		var rowLength = table.rows.length-1;
		$('#info').text("成功完成拓词，共拓展"+rowLength+"个词，您可以在此选择需要的关键词。");
		
	});
	
	$('#uploadbtn').click(function () {
		
            //$('input:checkbox:checked') 等同于 $('input[type=checkbox]:checked')
            //意思是选择被选中的checkbox
			var promotionPlanName = "";
            var promotionUnitName = "";
            var keyword = "";
       		var clickRate = "";
		    obj = document.getElementsByName("check");
		   
		    for(k in obj){
		        if(obj[k].checked){
		        	var index = obj[k].value;
		        	promotionPlanName = promotionPlanName + material[index].promotionPlanName+",";
		        	promotionUnitName = promotionUnitName + material[index].promotionUnitName+",";
		        	keyword = keyword+material[index].keyword+",";
		        	clickRate = clickRate+material[index].clickRate+",";
		        }  	
		    }
		    if(keyword==""||keyword==null){
		    	alert("请选择关键词");
		    	return;
		    }else{
		    promotionPlanName = promotionPlanName.substring(0,promotionPlanName.length-1);
		    promotionUnitName = promotionUnitName.substring(0,promotionUnitName.length-1);
		    keyword = keyword.substring(0,keyword.length-1);
		    clickRate = clickRate.substring(0,clickRate.length-1);				    		    
	        $.post('/generalizeSet',
	        		{
        		promotionPlanName:promotionPlanName,
        		promotionUnitName:promotionUnitName,
        		keyword:keyword,
        		clickRate:clickRate
        	},	
	        	function(result){
	        	
	        	if(result.result=="success"){
	        		window.location.href=encodeURI("generalize_set.html");	        		
	        	}else{
	        		alert("请选择关键词数据");
	        	}
	        })	        
		    }
	});
	$('#record').click(function(){
		window.location.href = encodeURI("uploading_record.html");
	});
	
})
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = decodeURI(window.location.search).substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}


function setIfOpen(val) {
    document.cookie = "ifOpen=" + val;
  }

  function getIfOpenVal(param) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split(";");
    for (var i = 0; i < arrCookie.length; i++) {
      var arr = arrCookie[i].split("=");
      if (arr[0] == param)
        return arr[1];
    }
    return null;
  }
//收缩服务
var ifOpen = getIfOpenVal("ifOpen");

// index页面默认展开浮窗
if(ifOpen == 0){
  $(".business-bridge-iframe").animate({
    left : "-230px",
  }, 0)
  $(".js-shrink-button").find("span").removeClass("control").text("服务指导");
  
  $(".js-shrink-button").toggle(function() {
    $(".business-bridge-iframe").animate({
      left : "0px",
    }, 1000)
    $(this).find("span").addClass("control").text("收起");
    setIfOpen(1);
  }, function() {

    $(".business-bridge-iframe").animate({
      left : "-230px",
    }, 1000)
    $(this).find("span").removeClass("control").text("服务指导");
    setIfOpen(0);
  })
}else{

  $(".js-shrink-button").toggle(function() {
    $(".business-bridge-iframe").animate({
      left : "-230px",
    }, 1000)
    $(this).find("span").removeClass("control").text("服务指导");
    setIfOpen(0);
  }, function() {

    $(".business-bridge-iframe").animate({
      left : "0px",
    }, 1000)
    $(this).find("span").addClass("control").text("收起");
    setIfOpen(1);
  })



}